// pages/my/my.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatar: '/images/my/tx.png',
        avatarmr: '/images/my/tx.png',
        // avatar: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
        nickname: '还未登录',
        introduce: '还没有设置个人介绍哦～',
        gender: 0,
        openid: null,
        gradetext: '艾灸新手',
        openid: null,
    },
    headerClick() {
        console.log('headerClick', this.data);
        if (!this.data.openid) {
            this.login();
        } else {
            this.gotoSetUserInfo();
        }
    },

    /**
     * 获取openid
     */
    wxlogin() {
        return new Promise((resolve, reject) => {
            wx.login({
                //成功放回
                success: (res) => {
                    let code = res.code;
                    console.log('code:', code)
                    // const appid = 'wx334836444f5fd67e';
                    // const secret = 'a0b01da4ed98f1bfcf96aea560bdb964';
                    // const appid = 'wx2ed3ab7ee8d54da4';
                    // const secret = '75c77ccd7ec64957c9755760a3d3a541';
                    // wx.request({
                    //     url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`,
                    //     success: (res) => {
                    //         // console.log(res);
                    //         console.log('res.data', res.data);
                    //         const {
                    //             openid,
                    //             session_key
                    //         } = res.data;
                    //         wx.setStorageSync('openid', openid)
                    //         this.setData({
                    //             openid,
                    //         });
                    //         resolve(openid);
                    //         wx.showToast({
                    //             title: '登录成功',
                    //         })
                    //     }
                    // })
                    const openid = code;
                    wx.setStorageSync('openid', openid)
                    this.setData({
                        openid,
                    });
                    resolve(openid);
                    // wx.showToast({
                    //     title: '登录成功',
                    // })
                }
            })
        })
    },

    async login() {
        const that = this;
        wx.showLoading();
        let openidLoc = wx.getStorageSync('openid');
        let userinfo = wx.getStorageSync('userinfo');
        let openid = null;
        if (!openidLoc) {
            // 还没有登录
            console.log('还没有登录');
            openid = await this.wxlogin();
            wx.setStorageSync('openid', openid)
            userinfo = {
                age: 18,
                sex: '女',
                avatar: '',
                gradenum: "1",
                introduce: '我轻轻的来,轻轻的走~',
                istest: '是',
                gradetext: "艾灸新手",
                integralnum: "10",
                nickname: "未设置昵称",
                openid,
                istest: '否'
            }
            wx.setStorageSync('userinfo', userinfo)
        } else {
            // 已经获取到了openid
            console.log('已经获取到了openid', openidLoc, userinfo);
            openid = openidLoc;
        }
        that.setData({
            nickname: userinfo.nickname,
            introduce: userinfo.introduce,
            avatar: userinfo.avatar,
            gradetext: userinfo.gradetext,
            openid,
        })
        wx.hideLoading();
        // const mytime = setTimeout(() => {
        //     wx.hideLoading();
        //     clearTimeout(mytime)
        // }, 400)
    },
    gotoMyDemo() {
        wx.navigateTo({
            url: '../../template/home/test/viewResults/viewResults',
        })
    },

    clickaijiujilu() {
        wx.navigateTo({
            url: './aijiujilu/aijiujilu',
        })
    },

    gotoyijianfankui() {
        // wx.showToast({
        //     title: '开发中...',
        //     icon: 'none'
        // })
        console.log('意见反馈')
        wx.navigateTo({
            url: './feedback/feedback',
        })
    },

    gotoWofabude() {
        wx.navigateTo({
            url: '../../template/my/wofabude/wofabude',
        })
    },

    gotoSetUserInfo() {
        wx.navigateTo({
            url: './userInfo/userInfo',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const openid = wx.getStorageSync('openid')
        if (openid) {
            const {
                avatar,
                gradetext,
                introduce,
                nickname,
                openid
            } = wx.getStorageSync('userinfo');
            this.setData({
                nickname,
                introduce,
                avatar,
                gradetext,
                openid,
            })
        } else {
            this.login()
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.login();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})